﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DAL;
using BLL;
using NLog;

namespace QLTTTH
{
    public partial class frmThongTinKhoaHoc : DevExpress.XtraEditors.XtraForm
    {

        private Logger logger = LogManager.GetCurrentClassLogger();

        public frmThongTinKhoaHoc()
        {
            InitializeComponent();
        }

        public void FillControl(KhoaHoc kh)
        {
            this.txtMaKH.Text = kh.MaKH.ToString();
            this.loupMonHoc.EditValue = kh.MaMH;
            this.txtTenKH.Text = kh.TenKH;
            this.dtpTgBatDau.EditValue = kh.ThoiGianBD;
            this.dtpTgKetThuc.EditValue = kh.ThoiGianKT;
        }

        public delegate void SendData(KhoaHoc kh);

        public SendData sendData;

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (sendData != null)
            {
                var vResult = new KhoaHocManager().Validate(GetKhoaHoc());
                if (vResult.Valid)
                {
                    sendData(GetKhoaHoc());
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    logger.Info(vResult.ToErrorString());
                    XtraMessageBox.Show(vResult.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private KhoaHoc GetKhoaHoc()
        {
            var kh = new KhoaHoc();
            if (!txtMaKH.Text.ToString().Equals(""))
            {
                kh.MaKH = int.Parse(txtMaKH.Text.ToString());
            }
            kh.MaMH = int.Parse(loupMonHoc.EditValue.ToString());
            kh.TenKH = txtTenKH.Text;
            kh.ThoiGianBD = DateTime.Parse(dtpTgBatDau.Text.ToString());
            kh.ThoiGianKT = DateTime.Parse(dtpTgKetThuc.Text.ToString());

            return kh;
        }

        private void frmThongTinKhoaHoc_Load(object sender, EventArgs e)
        {
            var aResult = MonHocManager.LayDanhSachMonHoc();
            if (aResult.Success)
            {
                loupMonHoc.Properties.DataSource = aResult.Result;
                loupMonHoc.Properties.ValueMember = "MaMH";
                loupMonHoc.Properties.DisplayMember = "TenMH";
                if (loupMonHoc.ItemIndex < 0)
                    loupMonHoc.ItemIndex = 0;
                if (string.IsNullOrEmpty(dtpTgBatDau.Text.ToString()))
                    dtpTgBatDau.EditValue = DateTime.Now;
                if (string.IsNullOrEmpty(dtpTgKetThuc.Text.ToString()))
                    dtpTgKetThuc.EditValue = DateTime.Now;
            }
        }

        private void btnHuyBo_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}